package com.leetcode.LC_MED;

import java.util.ArrayList;
import java.util.List;

public class LC113 {
    public List<List<Integer>> pathSum(TreeNode root, int targetSum) {

        ArrayList<List<Integer>> lists = new ArrayList<>();
        dfs(root, targetSum, lists, new ArrayList<>());
        return lists;

    }

    private void dfs(TreeNode root, int targetSum, List<List<Integer>> lists, List<Integer> list) {

        if (root == null) {
            return;
        }
        list.add(root.val);
        targetSum -= root.val;
        if (targetSum == 0 && root.right == null && root.left == null) {
            lists.add(new ArrayList<>(list));
        }
        //targetSum -= root.val;

        dfs(root.left, targetSum, lists, list);
        dfs(root.right, targetSum, lists, list);
        list.remove(list.size() - 1);
        //targetSum += root.val;

    }
}
